package pers.jg.rrsserver.controller

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.google.gson.GsonBuilder
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*
import pers.jg.rrsserver.entity.Goods
import pers.jg.rrsserver.mapper.GoodsMapper

@RestController
@CrossOrigin
class GoodsController {

    val dateFormat = "yyyy-MM-dd'T'HH:mm:sssZ"

    class GoodsList {
        lateinit var goodsList: ArrayList<Goods>
    }

    @Autowired
    private lateinit var goodsMapper: GoodsMapper

    @PostMapping("/api/postNewGoods")
    fun postNewGoods(
        @RequestBody data: String
    ): String {

        val gson = GsonBuilder()
            .setDateFormat(dateFormat)
            .create()
        val newGoods = gson.fromJson(data, Goods::class.java)
        println(newGoods.id)
        val res = goodsMapper.insert(newGoods)
        return res.toString()

    }

    @GetMapping("/api/getGoodsList")
    fun getGoodsList(
        @RequestParam(value = "key", defaultValue = "") key: String,
        @RequestParam(value = "orderBy", defaultValue = "") orderBy: String
    ): String {
        val queryWrapper = QueryWrapper<Goods>()
        if (key != "") {
            queryWrapper.like("name", key).or().like("introduction", key)
        }
        if(orderBy == ""){
            queryWrapper.orderByDesc("add_time")   // 按照时间降序排序
        }
        else{
            queryWrapper.orderByDesc(orderBy)   // 按照给定的字段降序排序
        }
        val goodsList = goodsMapper.selectList(queryWrapper)
        val gson = GsonBuilder()
            .create()
        return gson.toJson(goodsList)
    }


    @PostMapping("/api/updateGoods")
    fun updateGoods(
        @RequestBody data: String
    ): String {

        val gson = GsonBuilder()
            .setDateFormat(dateFormat)
            .create()
        val goods = gson.fromJson(data, Goods::class.java)
        println(goods.id)
        val res = goodsMapper.updateById(goods)
        return res.toString()
    }

    @PostMapping("/api/deleteGoods")
    fun deleteGoods(
        @RequestParam("id") id: String
    ): String {
        println(id)
        return goodsMapper.deleteById(id).toString()
    }

    @GetMapping("/api/getGoods")
    fun getGoods(
        @RequestParam("id") id: String
    ): String {
        val goods = goodsMapper.selectById(id)
        val gson = GsonBuilder()
            .create()
        return gson.toJson(goods)
    }

    @GetMapping("/api/getGoodsTypeList")
    fun getGoodsTypeList(): String {
        val queryWrapper = QueryWrapper<Goods>()
        queryWrapper
            .isNotNull("type")
            .select("DISTINCT type")

        //  返回 [{"type":"普通"},{"type":"豆类"},{"type":"果蔬"}]
        val typeListWithField = goodsMapper.selectList(queryWrapper)
        val gson = GsonBuilder()
           .create()

        //  返回 ["普通","豆类","果蔬"]
        val typeList = ArrayList<String>()
        for(type in typeListWithField){
            typeList.add(type.type!!)
        }

        return gson.toJson(typeList)
    }

    @GetMapping("/api/getSellerGoodsList")
    fun getSellerGoodsList(
        @RequestParam("sellerId") sellerId: Int
    ): String? {
        val queryWrapper = QueryWrapper<Goods>()
        queryWrapper.eq("seller_id", sellerId)
        queryWrapper.orderByDesc("add_time")
        val goodsList = goodsMapper.selectList(queryWrapper)
        val gson = GsonBuilder()
            .create()
        return gson.toJson(goodsList)
    }
}